package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.TimeData;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author Demon
 * @date 21/8/2024  下午2:01
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class GetTimeZone implements CompleteEntityScheduleActions {
	
	@Data
	@NoArgsConstructor
	@Builder
	public static class InputMsg implements Serializable {
	
	}
	
	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	@Builder
	public static class OutputMsg implements Serializable {
		/**
		 * 返回设置的时间时区
		 */
		private TimeData time;
	}
	
	private InputMsg input;
	private OutputMsg output;
	
	@Override
	public ByteString objectConvertBytes() {
		final CompleteInstruction.GetTimeZone.InputMsg.Builder inputMsg =
				CompleteInstruction.GetTimeZone.InputMsg.newBuilder();
		final CompleteInstruction.GetTimeZone.Builder builder =
				CompleteInstruction.GetTimeZone.newBuilder();
		builder.setInput(inputMsg);
		return builder.build().toByteString();
	}
	
	@Override
	public GetTimeZone byteConvertObject(ByteString bytes) {
		CompleteInstruction.GetTimeZone getTimeZone = null;
		try {
			getTimeZone = CompleteInstruction.GetTimeZone.parseFrom(bytes);
		} catch (InvalidProtocolBufferException e) {
			log.error("", e);
		}
		if (Objects.isNull(getTimeZone)) {
			throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
		}
		GetTimeZone.OutputMsg outputMsg = new GetTimeZone.OutputMsg();
		CompleteInstruction.GetTimeZone.OutputMsg output = getTimeZone.getOutput();
		outputMsg.setTime(new TimeData().formProto(output.getTime()));
		this.setOutput(outputMsg);
		return this;
	}
}

